/*
 * @Author: chenhuanhuan 2925102113@qq.com
 * @Date: 2024-12-13 19:23:23
 * @LastEditors: wuheng wuheng@szhbzc.com
 * @LastEditTime: 2024-12-24 09:18:41
 * @FilePath: \hbzc-groupmeal-center-web\src\utils\globalFn.ts
 */

import { getSpuClassTree, } from "@/api/Spu";
import { getSkuClassTree, } from "@/api/Sku";
import { reqAllMenuTreeApi } from '@/api/Newuser'
import { getRoleResourceTree } from "@/api/organizeFramework"
//获取SPU类别的树状结构
export const getSpuClassTreeApi = () => {
  return new Promise < void> ((resolve, reject) => {
    getSpuClassTree().then((res) => {
      if (res.code == 2000) {
        resolve(res.data);
      }
    });
  });
};

// 获取SKU类别的树壮结构
export const getSkuClassTreeApi = () => {
  return new Promise < void> ((resolve, reject) => {
    getSkuClassTree().then((res) => {
      if (res.code == 2000) {
        resolve(res.data);
      }
    });
  });
};
// 获取下级角色权限树  
export const getRsourceTreeApi = (fromTenantId: any) => {
  console.log(fromTenantId, 1111);

  return new Promise < void> ((resolve, reject) => {
    getRoleResourceTree({ fromTenantId }).then((res) => {
      if (res.code == 2000) {
        resolve(filterMenu(res.data));
      }
    });
  });
};
// 获取角色权限树  
export const getReqAllMenuTreeApi = () => {
  return new Promise < void> ((resolve, reject) => {
    reqAllMenuTreeApi({}).then((res) => {
      if (res.code == 2000) {


        resolve(filterMenu(res.data));
      }
    });
  });
};
function filterMenu(list) {

  let menuList = list
  menuList.forEach(it => {
    if (it.children) {
      if (it.children.length && it.resourceType == "menu") {
        it.buttonResourceList = it.children.map(item => {
          let { resourceName, permissionCode, resourceId } = item
          return {
            resourceId,
            resourceName,
            permissionCode,
            label: resourceName,
            value: permissionCode,

          }
        })
        it.children = []
      } else {
        it.children = filterMenu(it.children)
      }
    }
  });


  return menuList
}
